package com.amap.gunfighting;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.app.Activity;
import android.content.Intent;

public class Login extends Activity 
{
	private Button loginButton;
	public static EditText userName;
	public static EditText passWord;
	private TextView loginPoint;
	String strUrl = "http://83.251.86.183:8080/ZRTrafficWeb/trafficweb";
	//String strUrl = "http://213.103.200.57:8080/zr/trafficweb";
	//String strUrl = "http://localhost:8080/ZRTrafficWeb/trafficweb";
	URL url = null;
	
	public void onDestroy()
	{
		super.onDestroy();
	}
	public void onPause()
	{
		super.onPause();
	}
	public void onResume()
	{
		super.onResume();
	}

	
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        
        // get parameters from android application
        loginButton = (Button) findViewById(R.id.loginButton);
        userName = (EditText)findViewById(R.id.editText1);
        passWord = (EditText)findViewById(R.id.editText2);
        loginPoint = (TextView)this.findViewById(R.id.textView3);
        // set onClickListner, functions are not really developed for register part.
        loginButton.setOnClickListener(new SetLoginOnClickListener());
    }
    
    public class SetLoginOnClickListener implements OnClickListener
    {
		public void onClick(View arg0)
		{
    		try 
    		{				
    			url = new URL(strUrl);
    			HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
				
    			urlConn.setDoInput(true);
    			urlConn.setDoOutput(true);
    			urlConn.setRequestMethod("POST");
    			urlConn.setUseCaches(false);
    			urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    			urlConn.setRequestProperty("Charest", "utf-8");
    			// to connect to the server side
    			urlConn.connect();
    			
    			DataOutputStream dop = new DataOutputStream(urlConn.getOutputStream());
    			dop.writeBytes("point=" + URLEncoder.encode("login","utf-8"));
    			//it is essential that to add "&" to separate two strings
    			dop.writeBytes("&username=" + URLEncoder.encode(userName.getText().toString(),"utf-8"));
    			dop.writeBytes("&password=" + URLEncoder.encode(passWord.getText().toString(),"utf-8"));
    			dop.flush();
    			dop.close();
    			//get the feedback from the server whether the user's authority is approved 
    			DataInputStream dis = new DataInputStream(urlConn.getInputStream());
    			String loginPassage = dis.readUTF();
    			urlConn.disconnect(); // now???
    			loginPoint.setText(loginPassage);
    			// if feedback is succeed, application will jump to the second page of OSM map
    			if("Succeed".equalsIgnoreCase(loginPassage))
    			{
    				//this is the standard method for active the second activity. Do NOT write it into three steps.
    				startActivity(new Intent(Login.this, Map.class));
    				//finish the login activity
    				Login.this.finish();
    			}
    		} 
    		catch (MalformedURLException e) 
    		{
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} 
    		catch (IOException e)
    		{
    			e.printStackTrace();
    		}
		}
    }// end of SetLoginOnClickListener
}
